package com.kuqi.dao;

import com.kuqi.entity.UserInfo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import javax.transaction.Transactional;
import java.sql.Date;

public interface UserRepository extends JpaRepository<UserInfo, Integer> {

    // 根据账号查询用户信息
    @Query(value = "select * from userinfo where  apply_key =  ?1 limit 1",nativeQuery = true)
    UserInfo findByApplyKey(String applyKey);

    // 注册一个新的用户
    @Modifying
    @Transactional
    @Query(value = "INSERT into userinfo value(null,'unknown', 'unknown', :applyKey, :secretKey, :user_age_limit, :record_date, :effective, :user_img, '', 0, 0)", nativeQuery = true)
    int insertValue(@Param("applyKey")String applyKey, @Param("secretKey") String secretKey, @Param("user_age_limit") int user_age_limit,
                    @Param("record_date") Date record_date, @Param("effective") int effective, @Param("user_img") byte[] user_img);

    // 更换用户的头像
    @Modifying
    @Transactional
    @Query(value = "update userinfo set user_img = :user_img where apply_key = :applyKey", nativeQuery = true)
    int updateUserImg(@Param("applyKey")String applyKey, @Param("user_img") byte[] user_img);
}